package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.bsg.upm.constant.Messages;
import com.bsg.upm.entity.SanEntity;
import com.bsg.upm.entity.SanVendorEntity;
import com.bsg.upm.entity.SiteEntity;

/**
 * SAN业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class SanCheck extends BaseCheck {

	/**
	 * SAN新增时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * SAN删除时检查
	 * 
	 * @param san
	 *            SAN对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkRemove(SanEntity san) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (san == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "SAN"));
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 新增时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 站点非空检查
		String siteId = (String) paramMap.get("site");
		if (StringUtils.isBlank(siteId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "所属站点"));
			return chkRS;
		}

		// SAN名称非空和长度检查
		String name = (String) paramMap.get("name");
		if (StringUtils.isBlank(name)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "SAN名称"));
			return chkRS;
		}
		if (StringUtils.trim(name).length() > 64) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "SAN名称", 64));
			return chkRS;
		}

		// 品牌非空和长度检查
		String vendor = (String) paramMap.get("vendor");
		if (StringUtils.isBlank(vendor)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "品牌"));
			return chkRS;
		}

		// 管理域名非空和长度检查
		String domain = (String) paramMap.get("domain");
		if (StringUtils.isBlank(domain)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "管理域名"));
			return chkRS;
		}
		if (StringUtils.trim(domain).length() > 64) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "管理域名", 64));
			return chkRS;
		}

		// 起始LUN ID非空和有效性检查
		Integer lunStart = (Integer) paramMap.get("lunStart");
		if (lunStart == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "起始LUN ID"));
			return chkRS;
		}
		if (lunStart < 0 || lunStart > 10240) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.BETWEEN_MIN_AND_MAX, "起始LUN ID", 0, 10240));
			return chkRS;
		}

		// 结束LUN ID非空和有效性检查
		Integer lunEnd = (Integer) paramMap.get("lunEnd");
		if (lunEnd == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "结束LUN ID"));
			return chkRS;
		}
		if (lunEnd < 0 || lunEnd > 10240) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.BETWEEN_MIN_AND_MAX, "结束LUN ID", 0, 10240));
			return chkRS;
		}
		if (lunStart.intValue() > lunEnd.intValue()) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("起始LUN ID 不能大于 结束LUN ID");
			return chkRS;
		}

		// 起始主机LUN ID非空和有效性检查
		Integer hostLunStart = (Integer) paramMap.get("hostLunStart");
		if (hostLunStart == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "起始主机LUN ID"));
			return chkRS;
		}
		if (hostLunStart < 0 || hostLunStart > 10240) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.BETWEEN_MIN_AND_MAX, "起始主机LUN ID", 0, 10240));
			return chkRS;
		}

		// 结束主机LUN ID非空和有效性检查
		Integer hostLunEnd = (Integer) paramMap.get("hostLunEnd");
		if (hostLunEnd == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "结束主机LUN ID"));
			return chkRS;
		}
		if (hostLunEnd < 0 || hostLunEnd > 10240) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.BETWEEN_MIN_AND_MAX, "结束主机LUN ID", 0, 10240));
			return chkRS;
		}
		if (hostLunStart.intValue() > hostLunEnd.intValue()) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("起始主机LUN ID 不能大于 结束主机LUN ID");
			return chkRS;
		}

		if (paramMap.containsKey("description")) {
			// 描述长度检查
			String description = (String) paramMap.get("description");
			if (StringUtils.trim(description).length() > 256) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "描述", 256));
				return chkRS;
			}
		}

		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 站点存在性检查
		String siteId = (String) paramMap.get("site");
		SiteEntity site = siteDao.get(siteId);
		if (site == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "所属站点"));
			return chkRS;
		}

		// SAN名称重复性检查
		String name = (String) paramMap.get("name");
		if (sanDao.countBySiteIdAndName(name, siteId) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "站点下SAN名称"));
			return chkRS;
		}

		// 品牌存在性检查
		String vendor = (String) paramMap.get("vendor");
		SanVendorEntity sanVendor = sanVendorDao.get(vendor);
		if (sanVendor == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "品牌"));
			return chkRS;
		}
		return chkRS;
	}

}
